home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / sonido / sfxserve.000 / sfxserve / sfxserver-0.02 / channel.c next >
Encoding:
C/C++ Source or Header  |  1995-01-10  |  4.7 KB  |  188 lines

  1. /*
  2.  * ---------------------------------------------------------------------------
  3.  * sfxserver/channel.c
  4.  *
  5.  * Copyright by Terry Evans 1994
  6.  * tevans@cs.utah.edu, tevans@slc.unisys.com
  7.  * ---------------------------------------------------------------------------
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are
  11.  * met: 1. Redistributions of source code must retain the above copyright
  12.  * notice, this list of conditions and the following disclaimer. 2.
  13.  * Redistributions in binary form must reproduce the above copyright notice,
  14.  * this list of conditions and the following disclaimer in the documentation
  15.  * and/or other materials provided with the distribution.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  21.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  * ---------------------------------------------------------------------------
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33.  
  34.  
  35. #include "global.h"
  36. #include "types.h"
  37. #include "error.h"
  38. #include "io.h"
  39. #include "sample.h"
  40.  
  41.  
  42. /* Current channel number */
  43. int channel_number;
  44.  
  45.  
  46. void C_Init(channel_t **channel)
  47. {
  48.   int loop;
  49.  
  50.   IO_WriteStdout("        Setting up channel data structures\n");
  51.  
  52.   for(loop = 0; loop < MAX_CHANNELS; loop++)
  53.     channel[loop] = NULL;
  54.  
  55.   channel_number = 0;
  56. }
  57.  
  58.  
  59. void C_DeInit(channel_t **channel)
  60. {
  61.   int loop;
  62.  
  63.   for(loop = 0; loop < channel_number; loop++)
  64.   {
  65.     free(channel[loop]);
  66.     channel[loop] = NULL;
  67.   }
  68.  
  69.   /* Reset the channel number */
  70.   channel_number = 0;
  71. }
  72.  
  73.  
  74. int C_AllocChannel(channel_t **channel, sample_t *sample, int left, int right)
  75. {
  76.   int loop;
  77.  
  78.   for(loop = 0; loop < MAX_CHANNELS; loop++)
  79.   {
  80.     /* See if it is a free channel */
  81.     if(channel[loop] != NULL)
  82.     {
  83.       /* See if the channel is in use */
  84.       if(channel[loop]->in_use)
  85.     continue;
  86.  
  87.       else
  88.       {
  89.     /* The struct has already been allocated, so assign pointers */
  90.     channel[loop]->position = 0;
  91.     channel[loop]->vol_left = (float)left / 255.0;
  92.     channel[loop]->vol_right = (float)right / 255.0;
  93.     channel[loop]->in_use = TRUE;
  94.     channel[loop]->sample = sample;
  95.  
  96.     return(loop);
  97.       }
  98.     }
  99.  
  100.     /* We need to malloc the area and initialize */
  101.     else
  102.     {
  103.       if((channel[loop] = (channel_t *)malloc(sizeof(channel_t))) == NULL)
  104.     E_FatalError("C_AllocChannel", "could not malloc %ul bytes", sizeof(channel_t));
  105.  
  106.       channel[loop]->position = 0;
  107.       channel[loop]->vol_left = (float)left / 255.0;
  108.       channel[loop]->vol_right = (float)right / 255.0;
  109.       channel[loop]->in_use = TRUE;
  110.       channel[loop]->sample = sample;
  111.  
  112.       channel_number++;
  113.  
  114.       return(loop);
  115.     }
  116.   }
  117.  
  118.   /* There are no free channels */
  119.   return(-1);
  120. }
  121.  
  122.  
  123. int C_VerifyChannel(int number)
  124. {
  125.   if(number < channel_number)
  126.     return(TRUE);
  127.   else
  128.     return(FALSE);
  129. }
  130.  
  131.  
  132. int C_ChangeVolume(channel_t **channel, char side, int number, int volume)
  133. {
  134.   /* See if that channel is allocated */
  135.   if(!C_VerifyChannel(number))
  136.   {
  137.     E_NonFatalError("C_ChangeVolume", "channel %ul is not allocated", number);
  138.     return(-1);
  139.   }
  140.  
  141.   /* Make sure the space is allocated */
  142.   /* This error should NEVER occur */
  143.   if(channel[number] == NULL)
  144.   {
  145.     E_NonFatalError("C_ChangeVolume", "channel %ul is not allocated", number);
  146.     return(-1);
  147.   }
  148.  
  149.   /* See if that channel is in use */
  150.   if(channel[number]->in_use != TRUE)
  151.   {
  152.     E_NonFatalError("C_ChangeVolume", "channel %ul is not in use", number);
  153.     return(-1);
  154.   }
  155.  
  156.   if(side == 'l')
  157.     channel[number]->vol_left = (float)volume / 255.0;
  158.   else if(side == 'r')
  159.       channel[number]->vol_right = (float)volume / 255.0;
  160.   else
  161.   {
  162.     E_NonFatalError("C_ChangeVolume", "illegal side value '%c' for volume", side);
  163.     return(-1);
  164.   }
  165.  
  166.   return(volume);
  167. }
  168.  
  169.  
  170. int C_ChannelsInUse(channel_t **channel)
  171. {
  172.   int loop;
  173.   int num_channels = 0;
  174.  
  175.   for(loop = 0; loop < channel_number; loop++)
  176.   {
  177.     /* See if it is a free channel */
  178.     if(channel[loop] != NULL)
  179.     {
  180.       /* See if the channel is in use */
  181.       if(channel[loop]->in_use)
  182.     num_channels++;
  183.     }
  184.   }
  185.  
  186.   return(num_channels);
  187. }
  188.